package com.awesome.media;

import android.content.Context;
import android.net.Uri;
import android.util.Log;
import android.view.Surface;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import tv.danmaku.ijk.media.player.IMediaPlayer;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;

/**
 * Author: JFangZ
 * Email:janezhang1901@163.com
 * Date: 2020/4/29 14:24
 * Description:Ijk 引擎的播放器
 */
public class JeejioIjkPlayer extends AbsPlayer {
    private final IjkMediaPlayer mIjkMediaPlayer;

    public JeejioIjkPlayer(Context context) {
        super(context);
        mContext = context;
        mIjkMediaPlayer = new IjkMediaPlayer();

//        // 设置播放前的探测时间 1,达到首屏秒开效果
//        mIjkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "analyzeduration", 100L);
//        // 播放前的探测Size，默认是1M, 改小一点会出画面更快
//        mIjkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "probesize", 1024L);
//        // 每处理一个packet之后刷新io上下文
//        mIjkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "flush_packets", 1L);
//        // 是否开启预缓冲，一般直播项目会开启，达到秒开的效果，不过带来了播放丢帧卡顿的体验
//        mIjkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "packet-buffering", 0L);
//        // 视频帧处理不过来的时候丢弃一些帧达到同步的效果
//        mIjkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "framedrop", 5L);
//        // 缩短播放的rtmp视频延迟在1s内
//        mIjkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "fflags", "nobuffer");

        mIjkMediaPlayer.setVolume(1.0f, 1.0f);
        mIjkMediaPlayer.setOnPreparedListener(new IMediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(IMediaPlayer iMediaPlayer) {
                Log.i(TAG, "onPrepared");
                if (mMediaPlayerListener != null) {
                    mMediaPlayerListener.onPrepared();
                }
            }
        });
        mIjkMediaPlayer.setOnInfoListener(new IMediaPlayer.OnInfoListener() {
            @Override
            public boolean onInfo(IMediaPlayer iMediaPlayer, int what, int extra) {
                Log.i(TAG, "onInfo" + " : "
                        + "what--->" + what
                        + ",extra--->" + extra);
                if (what == IMediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) {
                    if (mMediaPlayerListener != null) {
                        mMediaPlayerListener.onVideoRenderingStart();
                    }
                }
                if (what == IMediaPlayer.MEDIA_INFO_BUFFERING_START) {
                    if (mMediaPlayerListener != null) {
                        mMediaPlayerListener.onBufferingStart();
                    }
                }
                if (what == IMediaPlayer.MEDIA_INFO_BUFFERING_END) {
                    if (mMediaPlayerListener != null) {
                        mMediaPlayerListener.onBufferingEnd();
                    }
                }
                return false;
            }
        });
        mIjkMediaPlayer.setOnErrorListener(new IMediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(IMediaPlayer iMediaPlayer, int what, int extra) {
                Log.i(TAG, "onError" + " : "
                        + "what--->" + what
                        + ",extra--->" + extra);
                if (mMediaPlayerListener != null) {
                    mMediaPlayerListener.onError(new Exception("播放失败"));
                }
                return true;
            }
        });
        mIjkMediaPlayer.setOnCompletionListener(new IMediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(IMediaPlayer iMediaPlayer) {
                Log.i(TAG, "onCompletion");
                mIjkMediaPlayer.reset();
                if (mMediaPlayerListener != null) {
                    mMediaPlayerListener.onComplete();
                }
            }
        });
        mIjkMediaPlayer.setOnVideoSizeChangedListener(new IMediaPlayer.OnVideoSizeChangedListener() {
            @Override
            public void onVideoSizeChanged(IMediaPlayer iMediaPlayer, int width, int height, int i2, int i3) {
                Log.i(TAG, "onVideoSizeChanged" + " : "
                        + "width--->" + width
                        + ",height--->" + height);
                if (mMediaPlayerListener != null) {
                    mMediaPlayerListener.onVideoSizeChanged(width, height);
                }
            }
        });
    }

    @Override
    public void setDisplay(Surface surface) {
        mIjkMediaPlayer.setSurface(mSurface = surface);
    }

    @Override
    public void play(Uri uri) {
        if (mScheduledFuture != null
                && !mScheduledFuture.isDone()
                && !mScheduledFuture.isCancelled()) {
            mScheduledFuture.cancel(true);
            mScheduledFuture = null;
        }
        mScheduledFuture = mScheduledExecutorService.schedule(new Runnable() {
            @Override
            public void run() {
                mIjkMediaPlayer.reset();
                mIjkMediaPlayer.setSurface(mSurface);
                try {
                    mIjkMediaPlayer.setDataSource(mContext, uri);
                } catch (IOException e) {
                    if (mMediaPlayerListener != null) {
                        mMediaPlayerListener.onError(e);
                    }
                    return;
                }
                mIjkMediaPlayer.prepareAsync();
            }
        }, 100, TimeUnit.MILLISECONDS);
    }

    @Override
    public void start() {
        mIjkMediaPlayer.start();
    }

    @Override
    public void pause() {
        mIjkMediaPlayer.pause();
    }

    @Override
    public void stop() {
        mIjkMediaPlayer.stop();
    }

    @Override
    public void reset() {
        mIjkMediaPlayer.reset();
    }

    @Override
    public void release() {
        mIjkMediaPlayer.release();
    }

    @Override
    public void seekTo(long position) {
        mIjkMediaPlayer.seekTo(position);
    }

    @Override
    public boolean isPlaying() {
        return mIjkMediaPlayer.isPlaying();
    }

    @Override
    public long getCurrentPosition() {
        return mIjkMediaPlayer.getCurrentPosition();
    }

    @Override
    public long getDuration() {
        return mIjkMediaPlayer.getDuration();
    }

    @Override
    public int getAudioSessionId() {
        return mIjkMediaPlayer == null ? -1 : mIjkMediaPlayer.getAudioSessionId();
    }

    @Override
    public int getVideoWidth() {
        return mIjkMediaPlayer.getVideoWidth();
    }

    @Override
    public int getVideoHeight() {
        return mIjkMediaPlayer.getVideoHeight();
    }

    @Override
    public void setVolume(float volume) {
        mIjkMediaPlayer.setVolume(volume, volume);
    }

    @Override
    public void setSpeed(float speed) {
        mIjkMediaPlayer.setSpeed(speed);
    }

    @Override
    public float getSpeed() {
        return mIjkMediaPlayer.getSpeed(0);
    }

    public IjkMediaPlayer getIjkMediaPlayer() {
        return mIjkMediaPlayer;
    }


}
